﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.CBO.SCM.Supplier;
    using UFIDA.U9.ISV.Customer.Proxy;
    using UFIDA.U9.ISV.Supplier;
    using UFIDA.U9.ISV.Supplier.Proxy;
    using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// SupplierSaveSV partial 
	/// </summary>	
	public partial class SupplierSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new SupplierSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class SupplierSaveSVImpementStrategy : BaseStrategy
	{
		public SupplierSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			SupplierSaveSV bpObj = (SupplierSaveSV)obj;
            try
            {
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");

				List<SupplierData> retlist = new List<SupplierData>();
				List<UFIDA.U9.ISV.Supplier.SupplierDTOData> inputdtos = new List<UFIDA.U9.ISV.Supplier.SupplierDTOData>();
                List<object> inputlist = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item=>item as object)
					, typeof(UFIDA.U9.ISV.Supplier.SupplierDTOData));
				foreach (UFIDA.U9.ISV.Supplier.SupplierDTOData dto in inputlist)
                {
					Supplier supplier = Supplier.Finder.Find($"Code='{dto.Code}' and Org={Context.LoginOrg.ID}");
					dto.Org = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData
					{
						ID = Context.LoginOrg.ID,
						Code = Context.LoginOrg.Code,
					};

					if(supplier == null)
                    {
						//新增逻辑
						BatchCreateSupplierByDTOSVProxy proxy = new BatchCreateSupplierByDTOSVProxy();
						proxy.ContextDTO = U9Utils.CreateContext();
						proxy.SupplierDTOs = new List<UFIDA.U9.ISV.Supplier.SupplierDTOData>() { dto };
                        List<SupplierData> ret = proxy.Do();
						if (ret.Count > 0)
							retlist.Add(ret[0]);
					}
                    else
                    {
						//修改逻辑
						if (supplier.SupplierSites != null)
						{
							//供应商位置 不空
							Dictionary<String, long> bedict = new Dictionary<string, long>();
                            foreach (SupplierSite site in supplier.SupplierSites)
							{
                                bedict.Add(site.Code, site.ID);
							}

                            foreach (UFIDA.U9.ISV.Supplier.SupplierSiteDTOData site in dto.SupplierSiteDTOs)
							{
								if (bedict.ContainsKey(site.Code))
								{
									site.OperateType = 1;//修改
									site.ID = bedict[site.Code];
								}
								else
								{
									site.OperateType = 0;//新增
								}
							}
						}
						if(supplier.SupplierBankAccount != null)
						{
                            //供应商银行 非空
                            Dictionary<String, long> bedict = new Dictionary<string, long>();
							foreach(SupplierBankAccount acc in supplier.SupplierBankAccount)
							{
								bedict.Add(acc.Code, acc.ID);
							}

							foreach(SupplierBankAccountDTOData acc in dto.BankAccountDTOS)
							{

                                if (bedict.ContainsKey(acc.Code))
								{
									acc.OperateType = 1;//修改
									acc.ID = bedict[acc.Code];
								}
								else
								{
									acc.OperateType = 0;//新增
								}
							}
                        }
						BatchModifySupplierByDTOSVProxy proxy = new BatchModifySupplierByDTOSVProxy();
						proxy.ContextDTO = U9Utils.CreateContext();
						proxy.SupplierDTOs = new List<UFIDA.U9.ISV.Supplier.SupplierDTOData> { dto };
                        List<SupplierData> ret = proxy.Do();
						if (ret.Count > 0)
							retlist.Add(ret[0]);
					}
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch(Exception ex)
            {
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}		
	}

	#endregion
	
	
}